<?php
	//Get category by id
	function get_category_by_id($id)
	{
		$query = "SELECT c.category_name,c.parent_id,c.state,c.cat_level,c.cat_path,c.description,c.display_order FROM categories as c WHERE c.id=$id";
		$result = mysql_query($query) or die(mysql_error());
		$row = array();
		$num_row = mysql_num_rows($result);
		if ($num_row !== 0)
			$row = mysql_fetch_array($result);
		return $row;
	}
	//Get categories' id by parent_id
	function get_categories_by_path($cat_path)
	{
		$query = "SELECT c.id FROM categories as c WHERE c.cat_path LIKE '$cat_path%' ORDER BY display_order";
		$result = mysql_query($query) or die(mysql_error());
		$rows = array();
		while ($row = mysql_fetch_array($result))
			$rows[] = $row;
		return $rows;
	}
	//Get parent_id by id
	function get_category_parent_id($id)
	{
		$query = "SELECT c.parent_id FROM categories as c WHERE c.id=$id";
		$result = mysql_query($query) or die(mysql_error());
		return mysql_result($result, 0);
	}
	//Get max display_order with parent_id
	function get_max_display_order($parent_path)
	{
		$query = "SELECT MAX(display_order) FROM categories WHERE cat_path LIKE '$parent_path%'";
		$result = mysql_query($query) or die(mysql_error());
		$max = mysql_result($result, 0);
		return $max;
	}
	//Get last display order
	function get_display_order_rank($parent_id,$display_order)
	{
		$query = "SELECT COUNT(id) FROM categories WHERE parent_id=$parent_id AND display_order<=$display_order";
		$result = mysql_query($query) or die(mysql_error());
		$rank = mysql_result($result, 0);
		return $rank;
	}
	//Count category childs
	function count_category_childs($parent_id)
	{
		$query = "SELECT COUNT(id) FROM categories WHERE parent_id=$parent_id";
		$result = mysql_query($query) or die(mysql_error());
		$count = mysql_result($result, 0);
		return $count;
	}
	//Update display_order
	function update_display_order($max_display_order,$value)
	{
		$query = "UPDATE categories SET display_order=display_order+$value WHERE display_order>$max_display_order";
		$result = mysql_query($query) or die(mysql_error());
		return mysql_affected_rows();
	}
	//Get category by name
	function get_category_by_name($name)
	{
		$query = "SELECT id,category_name,cat_level,cat_path FROM categories WHERE category_name='$name'";
		$result = mysql_query($query) or die(mysql_error());
		$row = array();
		$num_row = mysql_num_rows($result);
		if ($num_row !== 0)
			$row = mysql_fetch_array($result);
		return $row;
	}
	//Insert a new category
	function insert_category($name,$parent_id,$cat_level,$cat_path,$display_order,$description,$state,$created_by)
	{
		$query = "INSERT INTO categories (category_name,parent_id,cat_level,cat_path,display_order,state,description,created_by,created_on) 
					VALUES ('$name',$parent_id,$cat_level,'',$display_order,$state,'$description','$created_by',now())";
		$result = mysql_query($query) or die(mysql_error());
		$id = mysql_insert_id();
		//Update cat_path
		if ($id !== false && $id > 0) {
			$cat_path = $cat_path.$id.'-';
			$query = "UPDATE categories SET cat_path='$cat_path' WHERE id=$id";
			$result = mysql_query($query) or die(mysql_error());
			return mysql_affected_rows();
		}
		else {
			return 0;
		}
	}
	//Count posts in category
	function count_posts_in_category($cat_id)
	{
		$query = "SELECT COUNT(id) FROM posts WHERE cat_id=$cat_id";
		$result = mysql_query($query) or die(mysql_error());
		$count = mysql_result($result, 0);
		return $count;
	}
	//Count categories with condictions
	function count_cats_with_conditions($conditions)
	{
		$query = "SELECT COUNT(c.id) FROM categories as c $conditions";
		$result = mysql_query($query) or die(mysql_error());
		$count = mysql_result($result, 0);
		return $count;
	}
	//Get the categories with conditions and page
	function get_categories_with_conditions($from,$cats_per_page,$conditions)
	{
		$query = "SELECT c.id,c.category_name, c.cat_level,c.display_order,c.state,c.description,c.created_by,c.created_on 
			FROM categories as c $conditions 
			ORDER BY c.display_order 
			LIMIT $from,$cats_per_page";
		$result = mysql_query($query) or die(mysql_error());
		$rows = array();
		while ($row = mysql_fetch_array($result))
			$rows[] = $row;
		return $rows;
	}
	//Get the categories by ids
	function get_categories_by_ids($ids)
	{
		$query = "SELECT c.cat_path
			FROM categories as c  WHERE $ids";
		$result = mysql_query($query) or die(mysql_error());
		$rows = array();
		while ($row = mysql_fetch_array($result))
			$rows[] = $row;
		return $rows;
	}
	//Get all categories
	function get_all_categories()
	{
		$query = "SELECT c.id,c.category_name, c.cat_level,c.display_order,c.description,c.created_by,c.created_on FROM categories as c ORDER BY c.display_order";
		$result = mysql_query($query) or die(mysql_error());
		$rows = array();
		while ($row = mysql_fetch_array($result))
			$rows[] = $row;
		return $rows;
	}
	//Update categories state
	function update_categories_state($state,$cat_path)
	{
		$query = "UPDATE categories SET state=$state WHERE cat_path LIKE '$cat_path%'";
		$result = mysql_query($query) or die(mysql_error());
		return mysql_affected_rows();
	}
	//Update category info: name & description
	function update_category_info($id,$category_name,$description)
	{
		$query = "UPDATE categories SET category_name='$category_name',description='$description' WHERE id=$id";
		$result = mysql_query($query) or die(mysql_error());
		return mysql_affected_rows();
	}
	//Update the parent id
	function update_category_parent_id($id,$parent_id)
	{
		$query = "UPDATE categories SET parent_id=$parent_id WHERE id=$id";
		$result = mysql_query($query) or die(mysql_error());
		return mysql_affected_rows();
	}
	//Update category info: display_order, level, path, state
	function update_category_details($id,$level,$path,$state,$display_order)
	{
		$query = "UPDATE categories SET cat_level=$level,cat_path='$path',state=$state,display_order=$display_order WHERE id=$id";
		$result = mysql_query($query) or die(mysql_error());
		return mysql_affected_rows();
	}
	//Get the prev or next category
	function get_near_category($parent_id,$condition)
	{
		$query = "SELECT c.id FROM categories as c WHERE c.parent_id = $parent_id AND $condition LIMIT 0,1";
		$result = mysql_query($query) or die(mysql_error());
		$id = mysql_result($result, 0);
		return $id;
	}
	//Update the display by cat_path
	function update_display_order_by_cat_path($cat_path,$value)
	{
		$query = "UPDATE categories SET display_order=display_order+$value WHERE cat_path LIKE '$cat_path%'";
		$result = mysql_query($query) or die(mysql_error());
		return mysql_affected_rows();
	}
	//Sort the category by level
	function sort_categories_by_level($ids)
	{
		$query = "SELECT c.id FROM categories as c WHERE id IN $ids ORDER BY cat_level DESC";
		$result = mysql_query($query) or die(mysql_error());
		$rows = array();
		while ($row = mysql_fetch_array($result))
			$rows[] = $row;
		return $rows;
	}
	//Delete a category
	function delete_category($id) 
	{
		$query = "DELETE FROM categories WHERE id=$id";
		$result = mysql_query($query) or die(mysql_error());
		return mysql_affected_rows();
	}
 ?>